<template>
  <div>
    <overtime-header :data="data" @fetch="fetchData" />
    <record-list @refresh="fetchData" :data="data" v-if="total" />

    <img v-else src="./images/none.png" class="none-data">
  </div>
</template>

<script>
import OvertimeHeader from './OvertimeHeader.vue'
import RecordList from './RecordList.vue'

export default {
  components: {
    OvertimeHeader,
    RecordList
  },
  data() {
    const d = new Date()
    const year = d.getFullYear()
    const month = d.getMonth() + 1
    return {
      year,
      month: month > 9 ? month : `0${month}`,
      total: 0,
      data: []
    }
  },
  methods: {
    async fetchData(e = {}) {
      const { year = this.year, month = this.month } = e
      const { content, total } = await this.$http({
        url: '/api/overtime/record',
        params: {
          overtime_date: `${year}-${month}-01`
        }
      })
      this.data = content
      this.total = total
    }
  },
  mounted() {
    this.fetchData()
  }
}
</script>

<style lang="scss" scoped>
.none-data {
  width: 100%;
}
</style>
